<template>
  <el-dialog iconClass="el-icon-info"
    :title="item.id?'编辑':'新增'+'轮播图'"
    width="60%"
    :visible.sync="isDialog"
    @close="close">

    <el-form :model="item"
      :rules="rules"
      ref="form"
      class="demo-ruleForm"
      label-position="right"
      label-width="100px">
      <el-form-item label="排序权重">
        <template>
          <div class="block">
            <el-slider :max=255
              v-model="item.sort"
              show-input>
            </el-slider>
          </div>
        </template>
      </el-form-item>
      <el-form-item label="上传图">
        <el-upload class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="item.img"
            :src="item.img"
            class="avatar">
          <i v-else
            class="el-icon-plus avatar-uploader-icon"></i>
          <div><i class="el-icon-info"></i><span>点击图片重新上传</span></div>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary"
        @click="isDialog = false">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ['item'],
  data() {
    return {
      isDialog: true,
      tabs: 0,
      todo: '',
      title: '',
      rules: {}
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    close() {
      this.$emit('close')
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar {
  width: 200px;
  height: 200px;
}
.u_editor {
  height: 500px;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 200px;
  height: 200px;
  line-height: 200px;
  text-align: center;
}
</style>
